package com.tangyuan.video.config;

import cn.hutool.core.codec.Base64;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap;

/**
 * @author : zanglikun
 * @date : 2021/11/18 9:40
 * @Version: 1.0
 * @Desc : 苹果支付 参考地址：https://www.cnblogs.com/shoshana-kong/p/10991753.html
 * sendHttpsCoon 方法里面包含了增加的业务逻辑
 */
@Slf4j
@Controller
@RequestMapping("applePay")
public class ApplePay {

    //购买凭证验证地址
    private static final String certificateUrl = "https://buy.itunes.apple.com/verifyReceipt";

    //测试的购买凭证验证地址
    private static final String certificateUrlTest = "https://sandbox.itunes.apple.com/verifyReceipt";

    /**
     * 重写X509TrustManager
     */
    private static TrustManager myX509TrustManager = new X509TrustManager() {
        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }
    };

    /**
     * 接收自己APP客户端发过来的购买凭证
     *
     * @param userId    用户ID
     * @param receipt   苹果传递前端给的值
     * @param chooseEnv 是否时测试环境
     */
    @PostMapping("/setIapCertificate")
    @ResponseBody
    @Transactional
    public String setIapCertificate(@ApiParam(value = "用户ID") String userId, @ApiParam(value = "苹果传递前端支付成功的值") String receipt, @ApiParam(value = "是否是真实环境,布尔值") boolean chooseEnv) {
        //log.info("IOS端发送的购买凭证。数据有 userId = {},receipt = {},chooseEnv = {}",userId,receipt,chooseEnv);
        if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(receipt)) {
            return "用户ID 或者 receipt为空";
        }
        String url = null;
        url = chooseEnv == true ? certificateUrl : certificateUrlTest;
        final String certificateCode = receipt;
        if (StringUtils.isNotEmpty(certificateCode)) {
            String s = sendHttpsCoon(url, certificateCode, userId);
            if ("支付成功".equals(s)) {
                return s;
            } else {
                return s;
            }
        } else {
            return "receipt 为空!";
        }
    }

    /**
     * 发送请求 向苹果发起验证支付请求是否有效：本方法有认证方法进行调用
     *
     * @param url  支付的环境校验
     * @param code 接口传递的 receipt
     * @return 结果
     */
    private String sendHttpsCoon(String url, String code, String userId) {
        if (url.isEmpty()) {
            return null;
        }
        try {
            //设置SSLContext
            SSLContext ssl = SSLContext.getInstance("SSL");
            ssl.init(null, new TrustManager[]{myX509TrustManager}, null);

            //打开连接
            HttpsURLConnection conn = (HttpsURLConnection) new URL(url).openConnection();
            //设置套接工厂
            conn.setSSLSocketFactory(ssl.getSocketFactory());
            //加入数据
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-type", "application/json");


            JSONObject obj = new JSONObject();
            obj.put("receipt-data", code);

            BufferedOutputStream buffOutStr = new BufferedOutputStream(conn.getOutputStream());
            buffOutStr.write(obj.toString().getBytes());
            buffOutStr.flush();
            buffOutStr.close();
            //获取输入流
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

            String line = null;
            StringBuffer sb = new StringBuffer();
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            // 错误的 sb对象是：{"status":21002},苹果官网写的错误也都是2XXXX 具体含义可查：https://developer.apple.com/documentation/appstorereceipts/status
            // 所以 通过长度等于16，我们就可确定苹果支付成功是否有效
            if (sb.length() == 16) {
                return "支付失败,苹果说status异常";
            }
            // 将一个Json对象转成一个HashMap 
            JSONObject alljsoncode = JSON.parseObject(sb.toString());
            Object receipt = alljsoncode.get("receipt");
            HashMap hashMap = JSONUtil.toBean(receipt.toString(), HashMap.class);
            // 苹果给的订单号
            String original_transaction_id = (String) hashMap.get("original_transaction_id");
            //TODO 存储订单ID，并检查此订单ID是否存在，如果存在就证明已经发货了(避免二次发货)
            if ("com.hefeixunliao.zhenliao12yuan".equals(hashMap.get("product_id"))) {
                //TODO 执行12元的钻石增加
                log.info("用户ID：{},执行12元钻石追加", userId);
            } else if ("com.hefeixunliao.zhenliao30yuan".equals(hashMap.get("product_id"))) {
                //TODO 执行30元的钻石增加
                log.info("用户ID：{},执行30元钻石追加", userId);
            } else {
                log.info("用户ID：{},向苹果发起验证支付请求没有次product_id", userId);
                return "支付失败,当前没有次product_id";
            }
            return "支付成功";
        } catch (Exception e) {
            log.error("向苹果发起验证支付请求是否有效出现异常：{}", e.getMessage());
            return "支付过程中,出现了异常!";
        }
    }


    /**
     * 注意：下面代码跟苹果支付业务无关。
     * 这里的code 是前端请求苹果，苹果给前端的一个密钥（如果我们通过base64解密后，可获得signature、purchase-info、environment、pod、signing-status）这个密钥用于告诉Java服务器 想苹果服务器校验订单是否成功的参数
     */
    @Test
    public void jiemi() {
        String code = "ewoJInNpZ25hdHVyZSIgPSAiQXhmVWRiYUx5T2I5bllOM3hINmQzMnBaOHI2THdmV3ZmZ1NKN1o2QTM4dEY2SjNyUTZoRVZqQ3Rra01wMnhmM1pwWnFQRmw3ZlRIdDVxNVpKZUF6UWh4NWQ1djJrR01uM3NKb3ZBWXNuWENxY3VqclBWU3A5WTFYUTZjeTlvbVNORWNYVWt0L1dkQXhsRmN6WDRZMTJzcktsMDc3WHJIdk5JMDd0VTZXajgzbVdDNE1HZmF0c2E2UEo1RG5sT2lEOG96RlJ6a0NIQ3Y3bncvRm80dnFCaFpZRmlQSDZzeW1uN2lUQlhTcXlTdlJOTGJXLytUWktKZngxR1dRV3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